package com.hui.mapper;

import com.hui.dto.AssetsBorrowPageDTO;
import com.hui.entity.Assets;
import com.hui.entity.AssetsBorrow;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface AssetsBorrowMapper {

    @Insert("""
            insert into rms_assets_borrow (fk_assets_id, fk_emp_id, count,borrow_time, expected_return_time, return_time) 
            values (#{fkAssetsId}, #{fkEmpId}, #{count},#{borrowTime}, #{expectedReturnTime}, #{returnTime})
            """)
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(AssetsBorrow assetsBorrow);


    @Results(
            id = "assetsBorrowResultMap",
            value = {
                    @Result(property = "id", column = "id", id = true),
                    @Result(property = "fkAssetsId", column = "fk_assets_id"),
                    @Result(property = "fkEmpId", column = "fk_emp_id"),
                    @Result(property = "assets", column = "fk_assets_id", one = @One(select = "com.hui.mapper.AssetsMapper.select")),
                    @Result(property = "emp", column = "fk_emp_id", one = @One(select = "com.hui.mapper.EmpMapper.select"))
            }
    )
    @Select("""
            select * from rms_assets_borrow t
            where t.id = #{param1} and t.deleted = 0
            """)
    AssetsBorrow select(Long id);

    @ResultMap("assetsBorrowResultMap")
    @Select("""
            <script>
            select * from rms_assets_borrow t
            <where>
            <if test='fkAssetsId != null'> fk_assets_id = #{fkAssetsId} and </if>
            <if test='fkEmpId != null'> fk_emp_id = #{fkEmpId} and </if>
            t.deleted = 0
            </where>
            </script>
            """)
    List<AssetsBorrow> list(AssetsBorrowPageDTO dto);

    @Update("""
            <script>
            update rms_assets_borrow
            <set>
            <if test='id != null'> id = #{id},</if>
            <if test='fkAssetsId != null'> fk_assets_id = #{fkAssetsId},</if>
            <if test='fkEmpId != null'> fk_emp_id = #{fkEmpId},</if>
            <if test='count != null'> count = #{count},</if>
            <if test='borrowTime != null'> borrow_time = #{borrowTime},</if>
            <if test='expectedReturnTime != null'> expected_return_time = #{expectedReturnTime},</if>
            <if test='returnTime != null'> return_time = #{returnTime},</if>
            <if test='info != null'> info = #{info},</if>
            <if test='updated != null'> updated = #{updated},</if>
            <if test='deleted != null'> deleted = #{deleted},</if>
            <if test='version != null'> version = #{version},</if>
            version = version + 1
            </set>
            where id = #{id} and deleted = 0 and version = #{version} 
            </script>
            """)
    int update(AssetsBorrow assetsBorrow);

    @Update("""
            update rms_assets_borrow
            set deleted = 1, updated = current_timestamp
            where id = #{id} 
            """)
    int delete(Long id);

    @Update("""
            <script>
            update rms_assets_borrow set deleted = 1, updated = current_timestamp
            where
            <foreach collection='list'  item='e'  open='id in(' close=')' separator=','>
            #{e}
            </foreach>
            </script>
            """)
    int deleteBatch(List<Long> ids);



}
